/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.mad.bikepony.service.impl;

import java.util.Calendar;
import java.util.GregorianCalendar;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.jsonwebservice.JSONWebService;
import com.mad.bikepony.NoSuchDeliveryException;
import com.mad.bikepony.model.Agent;
import com.mad.bikepony.model.Delivery;
import com.mad.bikepony.model.DeliveryState;
import com.mad.bikepony.model.ResultMessage;
import com.mad.bikepony.service.base.DeliveryServiceBaseImpl;

/**
 * The implementation of the delivery remote service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.mad.bikepony.service.DeliveryService} interface.
 *
 * <p>
 * This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
 * </p>
 *
 * @author nicola
 * @see com.mad.bikepony.service.base.DeliveryServiceBaseImpl
 * @see com.mad.bikepony.service.DeliveryServiceUtil
 */
public class DeliveryServiceImpl extends DeliveryServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.mad.bikepony.service.DeliveryServiceUtil} to access the delivery remote service.
	 */
	
	@JSONWebService("/delivery/search-by-trackingcode")
	public ResultMessage searchDeliveryByTrackingCode(String trackingCode)
	{
		ResultMessage message = new ResultMessage();
		message.setStatusCode(0);
		message.setMessage("ok");
		try {
			Delivery delivery = deliveryPersistence.findByTrackingCode(trackingCode);
			JSONObject obj = JSONFactoryUtil.createJSONObject();
			DeliveryState state = DeliveryState.fromState(delivery.getState());
			obj.put("stateDescription", state.getDescription());
			obj.put("stateCode", state.toState());
			
			Calendar cal = GregorianCalendar.getInstance();
			switch (state) {
				case WAITING_FOR_PICKUP:
					cal.setTimeInMillis(delivery.getEstimatedPickupTime());
					cal.add(Calendar.MINUTE, 30);// sets a margin of confidence
					obj.put("estimatedPickupTime",cal.getTimeInMillis());
					obj.put("address", delivery.getSenderAddress());
					message.setData(obj);
					break;
				case BEING_DELIVERED:
					obj.put("pickupTimestamp", delivery.getPickupTimestamp());
					cal.setTimeInMillis(delivery.getEstimatedDeliveryTime());
					cal.add(Calendar.MINUTE, 30);// sets a margin of confidence
					obj.put("estimatedDeliveryTime",cal.getTimeInMillis());
					try {
						Agent agent = delivery.getAgent();
						obj.put("parcelLat", agent.getLastPos_latitude());
						obj.put("parcelLon", agent.getLastPos_longitude());
					} catch (PortalException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					message.setData(obj);
					break;
				case DELIVERED:
					obj.put("pickupTimestamp", delivery.getPickupTimestamp());
					obj.put("deliveryTimestamp", delivery.getDeliveryTimestamp());
					obj.put("address", delivery.getRecipientAddress());
					obj.put("signature", delivery.getRecipientSignature());
					message.setData(obj);
					break;
				default:
					
					break;
			}
			
		} catch (NoSuchDeliveryException e) {
			e.printStackTrace();
			message.setStatusCode(-1);
			message.setMessage("wrong tracking code");
		} catch (SystemException e) {
			e.printStackTrace();
			message.setStatusCode(-1);
			message.setMessage("unable to find the delivery. please Retry later.");
		}
		return message;
	}
}